package com.ma.controller;

import com.alibaba.fastjson.JSON;
import com.ma.pojo.Employees;
import com.ma.service.employees.EmployeesService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;
/**
*
*
@Author: xiaoma
@Date: 10:02 2020/8/26
@Description: 员工登陆信息
*/
@Controller
@RequestMapping("/employees")
public class UserLoginController {
    private final EmployeesService employeesService;

    public UserLoginController(EmployeesService employeesService) {
        this.employeesService = employeesService;
    }

    @RequestMapping("/goLogin")
    public String goLogin(){

        return "login";
    }
    @RequestMapping("/login")
    public String login(HttpSession session, String name, String password, Model model){
        System.out.println("======Login======");
        String info = "";
        System.out.println(name);
        System.out.println(password);
        Employees employees = employeesService.getLoginUser(name);
        System.out.println(employees);
        session.setAttribute("employees",employees);
        if (employees.getName() != null){
            info = "OK";
        }else {
            info = "无此用户！！";
        }
        if (employees.getPassword().equals(password)){
            model.addAttribute("msg","登陆成功");
            info = "密码输入正确";
            return "redirect:/employees/main"+info;
        }
        info = "密码输入有误！";
        Model msg = model.addAttribute("msg", "请重试！");
        System.out.println(msg);
        return "redirect:/employees/goLogin"+info;
    }
//    @CrossOrigin
//    @RequestMapping("/login2")
//    public Object login2(HttpSession session, String name, String password, Model model){
//        String msg = "";
//        Employees loginUser = employeesService.getLoginUser(name);
//        if(loginUser.getName().equals(name)){
//            msg = "OK";
//        }else{
//            msg = "用户不存在";
//        }
//        if (loginUser.getPassword().equals(password)){
//            msg = "OK";
//
//        }else{
//            msg = "密码错误";
//        }
//        Object json = JSON.toJSON(msg);
//        return json;
//    }

    @RequestMapping("/main")
    public String main(){
        return "main";
    }
    @RequestMapping("self")
    public String self(HttpSession session,Model model){
        System.out.println("========Self=======");
        Employees employees = (Employees) session.getAttribute("employees");
        List<Employees> list = new ArrayList<>();
        list.add(employees);
        System.out.println(employees);
        System.out.println(model.addAttribute("list", list));
        System.out.println(model.getAttribute("list"));
        return "self";
    }
    @RequestMapping("/toUpdateEmployees")
    public String toUpdateEmployee(HttpSession session,String name,Model model ){
        System.out.println("========toUpdateEmployees======");
        Employees employees = (Employees) session.getAttribute("employees");
        Model model1 = model.addAttribute("employees", employees);
        System.out.println(model1);
        return "updateEmployees";
    }
    @RequestMapping("/UpdateEmployees")
    public String updateEmployees(HttpSession session,Employees employees,Model model){
        System.out.println("=======UpdateEmployees======");
        employeesService.updateEmployees(employees);
        Employees employees1 = employeesService.getLoginUser(employees.getName());
        model.addAttribute("employees",employees1);
        session.setAttribute("employees",employees1);
        System.out.println(employees);
        return "redirect:/employees/main";
    }
    @RequestMapping("/logout")
    public String logout(HttpSession session){
        System.out.println("========logout=======");
        session.invalidate();
        return "main";

    }

}
